package sample

import (
	"organoid_public/domain/sample/service"
	"organoid_public/global"
	eventbus "organoid_public/infrastructure/event"
)

type UseCase struct {
	sub         eventbus.Subscribe
	SearchSvc   service.SearchServiceInterface
	ProtocolSvc service.ProtocolServiceInterface
	BrowseSvc   service.BrowseServiceInterface
	UserSvc     service.UserServiceInterface
}

func initSampleUseCase(sub eventbus.Subscribe, searchSvc service.SearchServiceInterface, protocolSvc service.ProtocolServiceInterface, browseSvc service.BrowseServiceInterface, userSvc service.UserServiceInterface) *UseCase {
	return &UseCase{sub: sub, SearchSvc: searchSvc, ProtocolSvc: protocolSvc, BrowseSvc: browseSvc, UserSvc: userSvc}
}

func wireService() (*UseCase, error) {
	defaultDb := global.GormDb.GetDb()
	log := global.Log
	uc, cleanup, err := wireApp(defaultDb, global.EventManager, log)
	if err != nil {
		log.Error("module", "sample", "func", "wireService", "error", err.Error())
		return nil, err
	}
	defer cleanup()

	return uc, err
}

func EventSampleSubscribe() (eventbus.Subscribe, error) {
	uc, err := wireService()
	if err != nil {
		return nil, err
	}

	return uc.sub, nil
}

// Service 获取领域服务实体
func Service() (*UseCase, error) {
	uc, err := wireService()
	return uc, err
}
